package com.huasheng.infohub.interceptors;



import java.util.Map;
import java.util.Map.Entry;

import com.huasheng.infohub.actions.BaseAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class UserAuthenticationInterceptor implements
		Interceptor {
	private static final long serialVersionUID = -47998736078335691L;

	@Override
	public void destroy() {
		// do nothing
	}

	@Override
	public void init() {
		// do nothing
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		boolean authed = false;
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		
		for(Entry<String, Object> entry: session.entrySet()) {
			if(entry.getKey().equals(BaseAction.USER_INFO)) {
				authed = true;
				break;
			} else {
				continue;
			}
		}
		
		return authed ? invocation.invoke() : BaseAction.LOGIN;
	}

}
